using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Preferences;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using NorthStar.Driver.Application;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;

namespace NorthStar.Driver
{
    public class AcceptBookingAsync : AsyncTask
    {
        private readonly NewWorkAvailableActivity _context;
        
        private ProgressDialog p;

      
        public AcceptBookingAsync(NewWorkAvailableActivity context)
        {

            _context = context;
            
            
        }

        protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
        {
            
            var api = new ConnectToSever(Helper.GetServer(_context));


            if (!string.IsNullOrEmpty(_context.bookingId)  && !string.IsNullOrEmpty(_context.driverId ))
            {
                //driver.driverStatus = status;
                return api.TryAcceptJob(_context.driverId, _context.bookingId);
            }
            return "";
        }

        protected override void OnPreExecute()
        {
            base.OnPreExecute();
            p = ProgressDialog.Show(_context, "Accepting Booking...", "Please Wait...", true, true);

        }

        protected override void OnPostExecute(Java.Lang.Object result)
        {
            base.OnPostExecute(result);
            //hide and kill the progress dialog
            p.Hide();
            p.Cancel();

            //setup to show the confirmation screen
           

            var res = Newtonsoft.Json.JsonConvert.DeserializeObject<GeneralResult>(result.ToString());
          
            if(res.Success)
            {

                var mact = new Intent(_context, typeof(JobingActivity));
                mact.PutExtra("driverID", _context.driverId);
                mact.PutExtra("bookingID", _context.bookingId);
                mact.PutExtra("address", _context.fullAddress);
                mact.PutExtra("customerPhoneNumber", _context.customerPhoneNumber);
                
                _context.StartActivity(mact);
                _context.Finish();
                
            }
            else
            {
                Intent a = new Intent(_context, typeof(MainActivity));
                a.AddFlags(ActivityFlags.ClearTop);
                a.PutExtra("driverID", _context.driverId);
                a.PutExtra("source", "tryAcceptBooking");
                _context.StartActivity(a);

            }

            

        }
    }
}